package com.epoint.mobile.demo.chart;


import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.AbstractChart;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.LineChart;
import org.achartengine.chart.PieChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.XYChart;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.tools.AlertHelp;

public class GraphicalActivity extends SuperView
{
	private int[] CC = new int[]{Color.BLUE,Color.GREEN,0xff336666,Color.RED,Color.CYAN,
			Color.LTGRAY,Color.MAGENTA,Color.GRAY,0xff66CC99,0xFFCC9966,0xFF999900,0xFF993300};
	
	private GraphicalView mView;
	private RelativeLayout rlChart;

	private double[] mailnum2012= new double[] { 
			34, 14, 41, 23, 56, 34, 18, 61, 45, 31,
			25, 9 };
	
	private double[] gwnum2012= new double[] { 
			23, 34, 24, 44, 12, 32, 11, 14, 21, 27,
			49, 51 };
	
	private String[] titiles = new String[]{"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
	@Override
	public void initControls(){
		rlChart = (RelativeLayout) findViewById(R.id.rlChart);
		mView = new GraphicalView(this, showBarChart());
		rlChart.addView(this.mView);
		
		top_right_bt.setVisibility(View.VISIBLE);
		top_right_bt.setText("图形");
		top_right_bt.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		if(v==top_right_bt)
		{
			AlertHelp.showAlertMenu(this, "选择图表类型", new String[]{"柱形图","饼图","线形图"}, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					rlChart.removeAllViews();
					switch (which) {
					case 0:
						mView = new GraphicalView(getContext(), showBarChart());
						rlChart.addView(mView);
						break;
					case 1:
						mView = new GraphicalView(getContext(), showPieChart());
						rlChart.addView(mView);
						break;
					case 2:
						mView = new GraphicalView(getContext(), showLineChart());
						rlChart.addView(mView);
					default:
						break;
					}
				}
			});
		}
	}
	
	private AbstractChart showLineChart()
	{
	    String[] titles = new String[] { "2012"};
	    List<double[]> x = new ArrayList<double[]>();
	    for (int i = 0; i < titles.length; i++) {
	      x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
	    }
	    
	    List<double[]> values = new ArrayList<double[]>();
	    values.add(mailnum2012);
//	    values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 });
//	    values.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 });
//	    values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });
	    
	    int[] colors = new int[] { Color.BLUE};
	    PointStyle[] styles = new PointStyle[] { PointStyle.TRIANGLE, PointStyle.DIAMOND,
	        PointStyle.TRIANGLE, PointStyle.SQUARE };
	    
	    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
	    
	    int length = renderer.getSeriesRendererCount();
	    for (int i = 0; i < length; i++) {
	      ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
	    }
	    setChartSettings(renderer, "2012年邮件数量统计", "月份", "邮件数量", 0.5, 12.5, 0, 70,
	        Color.BLACK, Color.BLACK);
	    renderer.setXLabels(12);
	    renderer.setYLabels(10);
	    renderer.setShowGrid(true);
	    renderer.setXLabelsAlign(Align.RIGHT);
	    renderer.setYLabelsAlign(Align.RIGHT);
	    renderer.setPanEnabled(false, false);
//	    renderer.setZoomButtonsVisible(true);
//	    renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
//	    renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
	    
	    XYChart chart = new LineChart(buildDataset(titles, x, values), renderer);
	    return chart;
//	    intent.putExtra(CHART, chart);
//	    intent.putExtra(TITLE, activityTitle);
//	    
//	    Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values),
//	        renderer, "Average temperature");
	  
	}
	
	private AbstractChart showPieChart()
	{
		String[] titles = titiles;
		double[] values = mailnum2012;
		
		int[] colors = new int[titles.length];
		int nums = titles.length/CC.length;
		if(nums==0){
			for(int i=0;i<titles.length;i++){
				colors[i] = CC[i];
			}
		}
		else{
			List<Integer> clist = new ArrayList<Integer>();
			for(int i=0;i<nums+1;i++){
				for(int col:CC){
					clist.add(col);
				}
			}
			int[] ccc = new int[clist.size()];
			for(int i=0;i<clist.size();i++){
				ccc[i] = clist.get(i);
			}
			for(int i=0;i<titles.length;i++){
				colors[i] = ccc[i];
			}
		}
		DefaultRenderer renderer = buildCategoryRenderer(colors);
//		renderer.setZoomButtonsVisible(true);
//		renderer.setZoomEnabled(true);
		renderer.setChartTitleTextSize(20);
		CategorySeries cs = buildCategoryDataset(titles, values);
		PieChart chart = new PieChart(cs, renderer);
		return chart;
	}

	private AbstractChart showBarChart()
	{
		String[] titles = new String[] { "2012"};
		List<double[]> values = new ArrayList<double[]>();
		values.add(mailnum2012);
		int[] colors = new int[] { 0xff0066FF};
		XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		setChartSettings(renderer, "2012年邮件数量统计", "月份", "邮件数量", 0.5,
				12.5, 0, 70, Color.BLACK, Color.BLACK);
		renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
		//    renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
		renderer.setXLabels(12);
		renderer.setYLabels(10);
		renderer.setXLabelsAlign(Align.LEFT);
		renderer.setYLabelsAlign(Align.LEFT);
		renderer.setPanEnabled(false, false);
		// renderer.setZoomEnabled(false);
		renderer.setZoomRate(1.1f);
		renderer.setBarSpacing(0.5f);//buildBarDataset(titles, values)
		BarChart chart = new BarChart(buildBarDataset(titles, values), renderer, Type.STACKED);
		return chart;
	}
	
	 protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
		    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		    setRenderer(renderer, colors, styles);
		    return renderer;
		  }
	 
	  protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
		    renderer.setAxisTitleTextSize(16);
		    renderer.setChartTitleTextSize(20);
		    renderer.setLabelsTextSize(15);
		    renderer.setLegendTextSize(15);
		    renderer.setPointSize(5f);
		    renderer.setMargins(new int[] { 20, 30, 15, 20 });
		    int length = colors.length;
		    for (int i = 0; i < length; i++) {
		      XYSeriesRenderer r = new XYSeriesRenderer();
		      r.setColor(colors[i]);
		      r.setPointStyle(styles[i]);
		      renderer.addSeriesRenderer(r);
		    }
		  }
	
	 protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues,
		      List<double[]> yValues) {
		    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		    addXYSeries(dataset, titles, xValues, yValues, 0);
		    return dataset;
		  }
	  public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
		      List<double[]> yValues, int scale) {
		    int length = titles.length;
		    for (int i = 0; i < length; i++) {
		      XYSeries series = new XYSeries(titles[i], scale);
		      double[] xV = xValues.get(i);
		      double[] yV = yValues.get(i);
		      int seriesLength = xV.length;
		      for (int k = 0; k < seriesLength; k++) {
		        series.add(xV[k], yV[k]);
		      }
		      dataset.addSeries(series);
		    }
		  }
	
	protected CategorySeries buildCategoryDataset(String[] titles, double[] values) {
		CategorySeries series = new CategorySeries("");
		for (int i=0;i<titles.length;i++) {
			series.add(titles[i], values[i]);
		}
		return series;
	}
	
	protected DefaultRenderer buildCategoryRenderer(int[] colors) {
		DefaultRenderer renderer = new DefaultRenderer();
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		renderer.setMargins(new int[] { 20, 30, 15, 0 });
		for (int color : colors) {
			SimpleSeriesRenderer r = new SimpleSeriesRenderer();
			r.setColor(color);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}

	protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		int length = titles.length;
		for (int i = 0; i < length; i++) {
			CategorySeries series = new CategorySeries(titles[i]);
			double[] v = values.get(i);
			int seriesLength = v.length;
			for (int k = 0; k < seriesLength; k++) {
				series.add(v[k]);
			}
			dataset.addSeries(series.toXYSeries());
		}
		return dataset;
	}

	protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
			String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
			int labelsColor) {
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		int length = colors.length;
		for (int i = 0; i < length; i++) {
			SimpleSeriesRenderer r = new SimpleSeriesRenderer();
			r.setColor(colors[i]);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}
	@Override
	public void refresh(Object... param) {
	}

	@Override
	public int getLayoutInt() {
		return R.layout.achartlayout;
	} 

	@Override
	public String setTitle() {
		return "图表统计";
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}
}